package com.gm.wholesale.module.good.ui.goodManger;

import android.text.InputFilter;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;

import com.gm.commonlib.module.evenbus.EventBusUtils;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.views.dialog.DialogUtil;
import com.gm.commonlib.views.dialog.InputDialog;
import com.gm.commonlib.views.superview.SuperTextView;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.module.kind.model.KindModel;
import com.gm.wholesale.module.kind.ui.KindListFragment;
import com.gm.wholesale.module.unit.model.UnitModel;
import com.gm.wholesale.module.unit.ui.UnitListFragment;

public class GoodMangerFragment extends BaseFragment {
    public FragmentManager mManager;
    public FragmentTransaction mTransaction;

    private RadioGroup goodRadioGroup;
    private SuperTextView createBtn;

    private int mCurrentTab = 0;
    //单位列表
    private UnitListFragment unitListFragment;
    //分类列表
    private KindListFragment mKindListFragment;
    //商品列表
    private GoodListFragment goodListFragment;


    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_good_manger, parent);
        mManager = getActivity().getSupportFragmentManager();
        initView(view);
        updateView();
    }

    private void initView(View view) {
        goodRadioGroup = view.findViewById(R.id.goodRadioGroup);
        createBtn = view.findViewById(R.id.createBtn);
        goodRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.goodRadioButton) {
                mCurrentTab = 0;
            } else if (checkedId == R.id.unitRadioButton) {
                mCurrentTab = 1;
            } else {
                mCurrentTab = 2;
            }
            updateView();
        });

        createBtn.setOnClickListener(v -> {
            if (mCurrentTab == 0) { //新增商品
                createGood();
            } else if (mCurrentTab == 1) { //创建单位
                createUnit();
            } else if (mCurrentTab == 2) { //新增分类
                createKind();
            }
        });
    }

    private void createUnit() {
      InputDialog dialog=  DialogUtil.showInputDialog(getActivity(), "新增单位", "", (inputData, inputDialog) -> {
          if (TextUtils.isEmpty(inputData)) {
              ToastUtil.showToast("单位名字不能为空");
              return;
          }
          ArrayMap<String, Object> unitData = new ArrayMap<>();
          unitData.put("name", inputData);
          UnitModel.createUnit((IBaseView) getActivity(), unitData, new BaseHttpObserver<>() {
              @Override
              protected void handleResultOkResp(BaseResp resp) {
                  ToastUtil.showToast("新增单位成功");
                  inputDialog.dismiss();
                  if (unitListFragment != null) {
                      unitListFragment.refreshData();
                  }
              }
          });
      });
        dialog.setEdInputFilter(new InputFilter[]{new InputFilter.LengthFilter(8)});
    }


    private void createKind() {
        InputDialog dialog=   DialogUtil.showInputDialog(getActivity(), "新增分类", "", (inputData, inputDialog) -> {
            if (TextUtils.isEmpty(inputData)) {
                ToastUtil.showToast("分类名字不能为空");
                return;
            }
            ArrayMap<String, Object> kindData = new ArrayMap<>();
            kindData.put("name", inputData);
            KindModel.createKind((IBaseView) getActivity(), kindData, new BaseHttpObserver<>() {
                @Override
                protected void handleResultOkResp(DataResp<Long> resp) {
                    ToastUtil.showToast("新增分类成功");
                    inputDialog.dismiss();
                    if (mKindListFragment != null) {
                        mKindListFragment.refreshData();
                    }
                }
            });
        });
        dialog.setEdInputFilter(new InputFilter[]{new InputFilter.LengthFilter(8)});
    }

    private void createGood(){

        EvenBusUtil.showCreateGoodUI(null);
    }


    private void updateView() {
        if (mCurrentTab == 0) {
            createBtn.setCenterString("新增商品");
            if (goodListFragment == null) {
                goodListFragment = new GoodListFragment();
                goodListFragment.setIsGetData(true);
            }
            goodListFragment.refreshData();
            showUI(goodListFragment);
            hidFragment(unitListFragment);
            hidFragment(mKindListFragment);
        } else if (mCurrentTab == 1) {
            createBtn.setCenterString("新增单位");
            if (unitListFragment == null) {
                unitListFragment = new UnitListFragment();
                unitListFragment.setIsGetData(true);
            }
            unitListFragment.refreshData();
            showUI(unitListFragment);
            hidFragment(goodListFragment);
            hidFragment(mKindListFragment);
        } else if (mCurrentTab == 2) {
            createBtn.setCenterString("新增分类");
            if (mKindListFragment == null) {
                mKindListFragment = new KindListFragment();
                mKindListFragment.setIsGetData(true);
            }
            mKindListFragment.refreshData();
            showUI(mKindListFragment);
            hidFragment(goodListFragment);
            hidFragment(unitListFragment);
        }
    }


    //改方法会销毁前面加载的fragment
    private void showUI(Fragment fragment) {
        if (mManager == null) return;
        mTransaction = mManager.beginTransaction();
        if (mTransaction == null) return;
        if (fragment == null) return;
        if (fragment.isAdded()) {
            mTransaction.show(fragment);
        } else {
            mTransaction.add(R.id.contentFL, fragment);
        }
        mTransaction.commitAllowingStateLoss();
    }

    private void hidFragment(Fragment fragment) {
        if (mManager == null) return;
        mTransaction = mManager.beginTransaction();
        if (mTransaction == null) return;
        if (fragment == null) return;
        mTransaction.hide(fragment);
        mTransaction.commitAllowingStateLoss();
    }

    private void removeFragment(Fragment fragment) {
        if (fragment == null) return;
        getActivity().getSupportFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();

    }

    @Override
    public void onDestroyView() {
        removeFragment(unitListFragment);
        removeFragment(mKindListFragment);
        removeFragment(goodListFragment);
        unitListFragment=null;
        mKindListFragment=null;
        goodListFragment=null;
        mCurrentTab=0;
        super.onDestroyView();
    }

}
